package com.first.agent.web.controller;

import com.alibaba.fastjson.JSONObject;
import com.first.agent.common.R;
import com.first.agent.common.R1;
import com.first.agent.pojo.Cloud;
import com.first.agent.pojo.gateway;
import com.first.agent.service.CloudService;
import com.first.agent.service.impl.CloudServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;

/**
 * @author 创建人名称
 * @version 1.0.0
 * @ClassName CloudServlet.java
 * @Description TODO
 * @createTime 2022年11月15日 11:22
 */
@WebServlet("/Cloud/*")
public class CloudServlet extends HttpServlet {
    private CloudService cloudService=new CloudServiceImpl();

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        /*
        获取请求路径
         */
        String requestURI = req.getRequestURI();
        String uri=requestURI.substring(7);
        /*
        门户管理默认页面显示数据和条件查询
         */
        if(uri.equals("selectGate")){
//            BufferedReader reader = req.getReader();
//            int a=0;
//            ArrayList<String> l1=new ArrayList<>();
//            while((a=reader.read())!=-1){
//                String s = reader.readLine();
//                l1.add(s);
//                System.out.println(s);
//            }
//            System.out.println(l1.get(0));
//            System.out.println(l1.get(1));
//            System.out.println(l1.get(2));
//            System.out.println(l1.get(3));
            String kw_name="null";
            String customer_name="null";
            kw_name = req.getParameter("kw_name");
            customer_name = req.getParameter("customer_name");
            System.out.println("关键字----"+kw_name);
            System.out.println("客户名称---------------"+customer_name);
            String page=req.getParameter("page");
            String limit = req.getParameter("limit");
            Integer limit1=Integer.valueOf(limit);
            Integer pageNumber=(Integer.valueOf(page)-1)*limit1;
            List<gateway> s1 = cloudService.selectgate(kw_name,customer_name,pageNumber,limit1);
            int count = cloudService.counta(kw_name,customer_name);
            R1 r=new R1();
            if(!s1.toString().equals("[]")){
                r.setCode(0);
                r.setMsg("查询成功");
                r.setCount(count);
                r.setData(s1);

            }else {
                r.setCode(500);
                r.setMsg("没有相关数据");
            }
            resp.setContentType("application/json;charset=utf-8");
            PrintWriter writer = resp.getWriter();
            writer.write(JSONObject.toJSONString(r));
            writer.flush();
            writer.close();
        }
        /*
        移动云端信息查询
         */
        if (uri.equals("selectCloud")) {
            List<Cloud> s1 = cloudService.selectCloud();
            R r=new R();
            if(!s1.toString().equals("[]")){
                r.setCode(200);
                r.setMsg("查询成功");
                r.setData(s1);
            }else {
                r.setCode(500);
                r.setMsg("系统出现问题");
            }
            resp.setContentType("application/json;charset=utf-8");
            PrintWriter writer = resp.getWriter();
            writer.write(JSONObject.toJSONString(r));
            writer.flush();
            writer.close();
        }
        /*
        移动云端修改
         */
        if(uri.equals("changeCloud")) {
            Cloud cloud = new Cloud();
            cloud.setCl_accout(req.getParameter("cl_accout"));
            cloud.setCl_password(req.getParameter("cl_password"));
            cloud.setCl_dacstios(req.getParameter("cl_dacstios"));
            cloud.setCl_qcdaios(req.getParameter("cl_qcdaios"));
            cloud.setCl_dacstand(req.getParameter("cl_dacstand"));
            cloud.setCl_qcdaand(req.getParameter("cl_qcdaand"));
            String cloud_id = req.getParameter("cloud_id");
            Integer c=Integer.valueOf(cloud_id);
            cloud.setCloud_id(c);
//            System.out.println(req.getParameter("cl_accout"));
//            System.out.println(req.getParameter("cl_dacstios"));
//            System.out.println(req.getParameter("cl_qcdaios"));
//            System.out.println(req.getParameter("cl_dacstand"));
//            System.out.println(req.getParameter("cl_qcdaand"));
            int i;
            i = cloudService.changeCloud(cloud);
            R r = new R();
            if (i != 0) {
                r.setCode(200);
                r.setMsg("修改成功");
            } else {
                r.setCode(500);
                r.setMsg("系统出现问题");
            }
            resp.setContentType("application/json;charset=utf-8");
            PrintWriter writer = resp.getWriter();
            writer.write(JSONObject.toJSONString(r));
            writer.flush();
            writer.close();
        }
        if(uri.equals("addCloud")) {
            Cloud cloud = new Cloud();
            cloud.setCl_accout(req.getParameter("cl_accout"));
            cloud.setCl_password(req.getParameter("cl_password"));
            cloud.setCl_dacstios(req.getParameter("cl_dacstios"));
            cloud.setCl_qcdaios(req.getParameter("cl_qcdaios"));
            cloud.setCl_dacstand(req.getParameter("cl_dacstand"));
            cloud.setCl_qcdaand(req.getParameter("cl_qcdaand"));
            cloud.setCl_address(req.getParameter("cl_address"));
//            System.out.println(req.getParameter("cl_accout"));
//            System.out.println(req.getParameter("cl_dacstios"));
//            System.out.println(req.getParameter("cl_qcdaios"));
//            System.out.println(req.getParameter("cl_dacstand"));
//            System.out.println(req.getParameter("cl_qcdaand"));
            int i;
            i = cloudService.addCloud(cloud);
            R r = new R();
            if (i != 0) {
                r.setCode(200);
                r.setMsg("添加成功");
            } else {
                r.setCode(500);
                r.setMsg("系统出现问题");
            }
            resp.setContentType("application/json;charset=utf-8");
            PrintWriter writer = resp.getWriter();
            writer.write(JSONObject.toJSONString(r));
            writer.flush();
            writer.close();
        }
    }


}
